package com.loie.xcx.web.controller;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.loie.xcx.aop.SignatureValidation;
import com.loie.xcx.comm.JsonResult;
import com.loie.xcx.config.TaxCloudConfig;
import com.loie.xcx.model.dto.*;
import com.loie.xcx.service.IwInstanceService;
import com.loie.xcx.service.SysAttachmentService;
import com.loie.xcx.util.AttachmentSave;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-03-01
 */

@RestController
@RequestMapping("/workflow")
@Slf4j
public class IwInstanceController {
    @Autowired
    private
    IwInstanceService iwInstanceService;
    @Autowired
    private
    SysAttachmentService sysAttachmentService;

    @Autowired
    private
    AttachmentSave attachmentSave;

    @Autowired
    private TaxCloudConfig taxCloudConfig;


    @SignatureValidation
    @PostMapping("/getFlowList")
    public
    JsonResult<IwInstanceListResultDTO> getQuotationList(@RequestBody String jsonStr) throws JsonProcessingException {
        IwinstanceQueryDTO iwinstanceQueryDTO = JSONObject.parseObject(jsonStr, IwinstanceQueryDTO.class);
        if (StringUtils.isNotBlank(iwinstanceQueryDTO.getOpenId()) && iwinstanceQueryDTO.getPageIndex() != null) {
            List<IwInstanceDTO> iwInstanceDTOList = iwInstanceService.getflowlist(taxCloudConfig.getModulelist(),iwinstanceQueryDTO);
            int size = iwInstanceService.getflowListTotal(taxCloudConfig.getModulelist(),iwinstanceQueryDTO);
            IwInstanceListResultDTO iwInstanceListResultDTO = new IwInstanceListResultDTO();
            iwInstanceListResultDTO.setFlowList(iwInstanceDTOList);
            iwInstanceListResultDTO.setTotal(size);

            JsonResult<IwInstanceListResultDTO> str=new JsonResult<>(iwInstanceListResultDTO);

            return new JsonResult<>(iwInstanceListResultDTO);
        }
        return new JsonResult<>(400, "查询失败");


    }
    @SignatureValidation
    @PostMapping("/getFlowCompletedList")
    public
    JsonResult<IwInstanceListResultDTO> getFlowCompletedList(@RequestBody String jsonStr) {
        IwinstanceQueryDTO iwinstanceQueryDTO = JSONObject.parseObject(jsonStr, IwinstanceQueryDTO.class);
        System.out.println(iwinstanceQueryDTO);
        if (StringUtils.isNotBlank(iwinstanceQueryDTO.getOpenId()) && iwinstanceQueryDTO.getPageIndex() != null) {
            List<IwInstanceDTO> iwInstanceDTOList = iwInstanceService.getflowcompletedlist(taxCloudConfig.getModulelist(),iwinstanceQueryDTO);
            //int size = iwInstanceDTOList.size();
            int size = iwInstanceService.getflowcompletedTotal(taxCloudConfig.getModulelist(),iwinstanceQueryDTO) ;
            IwInstanceListResultDTO iwInstanceListResultDTO = new IwInstanceListResultDTO();
            iwInstanceListResultDTO.setFlowList(iwInstanceDTOList);
            iwInstanceListResultDTO.setTotal(size);
            return new JsonResult<>(iwInstanceListResultDTO);
        }
        return new JsonResult<>(400, "查询失败");

    }


    @SignatureValidation
    @PostMapping("/flowApprove")
    public JsonResult<JSONObject> workflowApprove(@RequestBody String jsonStr)throws Exception{
        JSONObject msgJson= JSONObject.parseObject(jsonStr);
        String recordid =msgJson.getString("recordId");
        String subject=msgJson.getString("subject");
        String openId=msgJson.getString("openId");
        String modulename =msgJson.getString("modulename");
        Boolean isgree=msgJson.getBoolean("isgree");
        String username=msgJson.getString("username");
        String content=msgJson.getString("content");
        String spectime=msgJson.getString("spectime");

        String str=iwInstanceService.flow_agree(recordid,modulename,openId,isgree,username,content,spectime);
        if (StringUtils.isNotBlank(str)) {
            JSONObject jsonresult = new JSONObject();
            jsonresult.put("module",modulename);
            jsonresult.put("subject",subject);
            jsonresult.put("spresult",str);
            log.info(jsonresult.toString());
            return new JsonResult<>(jsonresult);
        }
        return new JsonResult<>(400, "审批流程处理失败");
    }

    @SignatureValidation
    @PostMapping("/getFlowModuleListTotal")
    public JsonResult<JSONObject> getFlowModuleListTotal(@RequestBody String jsonStr){
        IwinstanceQueryDTO iwinstanceQueryDTO = JSONObject.parseObject(jsonStr, IwinstanceQueryDTO.class);
        if (iwinstanceQueryDTO.getOpenId()!=null && StringUtils.isNotBlank(iwinstanceQueryDTO.getOpenId())) {
            List<Moduletotal> moduletotalList = iwInstanceService.getflowModuleListTotal(taxCloudConfig.getModulelist(), iwinstanceQueryDTO);
            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(moduletotalList));
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("modulelistTotal",jsonArray);
            return  new JsonResult<>(jsonObject);
        }

        return new JsonResult<>(400, "查询失败");
    }
    //根据InstanceID获得审批任务列表
    @SignatureValidation
    @PostMapping("/getFlowTaskList")
    public JsonResult<JSONObject> getFlowTaskList(@RequestBody String jsonStr){
        IwinstanceQueryDTO iwinstanceQueryDTO = JSONObject.parseObject(jsonStr, IwinstanceQueryDTO.class);
        if (iwinstanceQueryDTO.getInstanceID()!=null && StringUtils.isNotBlank(iwinstanceQueryDTO.getInstanceID())) {
            List<IwTaskDTO> taskList = iwInstanceService.getflowTaskList(iwinstanceQueryDTO);
            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(taskList));

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("tasklist",jsonArray);
            return  new JsonResult<>(jsonObject);
        }

        return new JsonResult<>(400, "查询失败");
    }
    //获取待审批总数
    @SignatureValidation
    @PostMapping("/getFlowListTotal")
    public JsonResult<JSONObject> getFlowListTotal(@RequestBody String jsonStr){
        IwinstanceQueryDTO iwinstanceQueryDTO = JSONObject.parseObject(jsonStr, IwinstanceQueryDTO.class);
        if (iwinstanceQueryDTO.getOpenId()!=null && StringUtils.isNotBlank(iwinstanceQueryDTO.getOpenId())) {
            Integer total=iwInstanceService.getflowListTotal(taxCloudConfig.getModulelist(),iwinstanceQueryDTO);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("Total",total);
            return new JsonResult<>(jsonObject);
        }
        return new JsonResult<>(400, "查询失败");
    }

    @SignatureValidation
    @PostMapping("/getAttachmentList")
    public  JsonResult<JSONObject> getAttachmentList(@RequestBody String jsonStr) {
        IwinstanceQueryDTO iwinstanceQueryDTO = JSONObject.parseObject(jsonStr, IwinstanceQueryDTO.class);
        if (StringUtils.isNotBlank(iwinstanceQueryDTO.getRecordId()) &&
                StringUtils.isNotBlank(iwinstanceQueryDTO.getModule())) {
            List<AttachmentVO> attachmentVOList=sysAttachmentService.getAttachmentList(iwinstanceQueryDTO);

            log.info(attachmentVOList.toString());
            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(attachmentVOList));

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("attachmentlist",jsonArray);
            jsonObject.put("Total",attachmentVOList.size());
            return new JsonResult<>(jsonObject);
        }

        return new JsonResult<>(400, "查询附件失败");


    }

    @SignatureValidation
    @PostMapping("/getAttachmentbase64")
    public  JsonResult<AttachmentDTO> getAttachmentbase64(@RequestBody String jsonStr) {
        IwinstanceQueryDTO iwinstanceQueryDTO = JSONObject.parseObject(jsonStr, IwinstanceQueryDTO.class);
        if (StringUtils.isNotBlank(iwinstanceQueryDTO.getRecordId()) &&
                StringUtils.isNotBlank(iwinstanceQueryDTO.getModule())) {
            AttachmentDTO attachmentDTO = attachmentSave.attachmenttobase64(iwinstanceQueryDTO);
            //AttachmentListDTO attachmentListDTO=new  AttachmentListDTO();
            //attachmentListDTO.setTotal(attachmentDTOList.size());
            // attachmentListDTO.setAttachmentList(attachmentDTOList);
            //System.out.println(attachmentListDTO);
            log.info(attachmentDTO.toString());
            return new JsonResult<>(attachmentDTO);
        }

        return new JsonResult<>(400, "查询附件失败");
    }







}

